package jvm.VMExecSubsystem;

/**
 * @author msi
 */
public class DeadLoopTestCase {

    static class DeadLoopClass {
        static {
            // 如果不加上这个if语句，编译器将提示“Initializer does not complete normally”并拒绝编译
            if (true) {
                System.out.println(Thread.currentThread() + "init DeadLoopClass");
                while (true) {
                }
            }
        }
    }

    public static void main(String[] args) {
        Runnable script = new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread() + "start");
                DeadLoopClass dlc = new DeadLoopClass();
                System.out.println(Thread.currentThread() + " run over");
            }
        };

        Thread thread1 = new Thread(script);
        Thread thread2 = new Thread(script);
        thread1.start();
        thread2.start();
    }

    /**
     * case 1: 非法向前引用变量
     */
    static {
        i = 0; //给变量赋值可以正常编译
//        System.out.println(i); //不能编译，会提示 Illegal forward reference
    }

    static int i = 1;

    /**
     * case 2: 父类的clinit方法总是先于子类的clinit方法执行
     */
    static class Parent {
        public static int A = 1;

        static {
            A = 2;
        }
    }

    static class Sub extends Parent{
        public static int B = A;
    }

//    public static void main(String[] args) {
//        System.out.println(Sub.B);  //值为2
//    }
}

